/**
 * 
 */
package com.gragra.sampling;
import java.util.Date;
import cern.jet.random.Uniform;
import cern.jet.random.engine.MersenneTwister;
/**
 * @author Christoph Teichmann
 * 06.06.2013
 */
public class ThreadedUniformAccess
{
	/**
	 * 
	 */
	public static void enableDateInit()
	{sameInit = false;}
	/**
	 * 
	 * @param val
	 * @return
	 */
	public static double setInit(int val)
	{
		sameInit = true;
		return initNum = val;
	}
	/**
	 * 
	 */
	private static int initNum = 830274723;
	/**
	 * 
	 */
	private static boolean sameInit = false;
	/**
	 * 
	 */
	private final Uniform[] engines;
	/**
	 * 
	 */
	public ThreadedUniformAccess(int threads)
	{
		super();
		this.engines = new Uniform[threads];
		for(int i=0;i<threads;++i)
		{
			this.engines[i] = new Uniform(sameInit ? new MersenneTwister(initNum) : new MersenneTwister(new Date()));
			for(int j=0;j<i+5;++j)
			{this.engines[i].nextDouble();}
		}
	}
	/**
	 * 
	 * @param forThread
	 * @return
	 */
	private Uniform getUniform(int forThread)
	{return engines[forThread];}
	/**
	 * 
	 * @param thread
	 * @return
	 */
	public double nextDouble(int thread)
	{return this.getUniform(thread).nextDouble();}
	/**
	 * 
	 * @param thread
	 * @return
	 */
	public int nextInt(int thread)
	{return this.getUniform(thread).nextInt();}
	/**
	 * 
	 * @param thread
	 * @param from
	 * @param to
	 * @return
	 */
	public double nextDouble(int thread,double from, double to)
	{return this.getUniform(thread).nextDoubleFromTo(from, to);}
	/**
	 * 
	 * @param thread
	 * @param from
	 * @param to
	 * @return
	 */
	public int nextInt(int thread,int from,int to)
	{return this.getUniform(thread).nextIntFromTo(from, to);}
	/**
	 * 
	 * @param number
	 * @return
	 */
	public boolean nextBoolean(int number)
	{return this.getUniform(number).nextBoolean();}
}